package visualizer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;

import processing.core.PGraphics;
import processing.core.PVector;
import visualizer.drawable.AbstractDrawable;
import visualizer.drawable.Drawable;
import visualizer.drawable.DrawableCollection;
import visualizer.graphics.HsbColor;

public class StatisticsVisualization extends AbstractDrawable {
	private DrawableCollection drawables = new DrawableCollection();
	private LinkedHashMap<String, Double> lhmAlaScaled = new LinkedHashMap();
	private LinkedHashMap<String, Double> lhmYlaScaled = new LinkedHashMap();
	HsbColor variFillYla = new HsbColor(0,255,220);
	HsbColor variFillAla = new HsbColor(128,255,220);
	HsbColor variStrokeYla = new HsbColor(255,255,0);
	HsbColor variStrokeAla = new HsbColor(255,255,0);

	public void draw(PGraphics g) {
		if (isEnabled()) {
			drawables.draw(g);
		}
	}

	public StatisticsVisualization(LinkedHashMap<String, Double> lhmYla, LinkedHashMap<String, Double> lhmAla, App a) {
		Bar bar = null;
		int x1 = 70;
		int x2 = 70;
		
		int otsikkojenLkm = lhmYla.size();
		
		//Skaalataan annetut arvot
		double biggest = checkTheBiggestValue(lhmYla, lhmAla);
		scaleHashMap(lhmYla, lhmYlaScaled, biggest);
		scaleHashMap(lhmAla, lhmAlaScaled, biggest);	

		Iterator ity = lhmYlaScaled.keySet().iterator();
		while(ity.hasNext()){   
			String key = (String) ity.next();
			double value = lhmYlaScaled.get(key);

			float width = 40;
			float height1 = (float) value;
			int y1 = (int) (a.height/2-height1);
			float depth = 20;
			HsbColor colorStroke1 = variStrokeYla;
			HsbColor color1 = variFillYla = variFillYla.addHue(16);
			boolean doubleBarUnder1 = false;
			String strl1 = key;

			bar = new Bar(x1, y1, width, height1, depth, color1, colorStroke1, strl1, a, doubleBarUnder1);
			drawables.add(bar);
			x1 += 120;
		}
		
		Iterator ita = lhmAlaScaled.keySet().iterator();
		while(ita.hasNext()){   
			String key = ita.next().toString();
			double value = lhmAlaScaled.get(key);

			float height2 = (float) value;
			int y2 = a.height/2;
			float width = 40;
			float depth = 20;
			HsbColor colorStroke2 = variStrokeAla;
			HsbColor color2 = variFillAla = variFillAla.addHue(16);
			boolean doubleBarUnder1 = true;
			String strl1 = key;

			bar = new Bar(x2, y2, width, height2, depth, color2, colorStroke2, strl1, a, doubleBarUnder1);

			if(bar != null){
				drawables.add(bar);
			}
			x2 += 120;
		}
	}
	
	private HashMap scaleHashMap(LinkedHashMap<String, Double> lhm, LinkedHashMap<String, Double> lhmKumpiScaled,double biggest) {
		Iterator it = lhm.keySet().iterator();
		while(it.hasNext()){   
			String key = (String) it.next();
			double value = lhm.get(key);
			double scaledValue = (value/biggest)*100;
			if(scaledValue < 5){
				scaledValue = 5;
			}
			lhmKumpiScaled.put(key, scaledValue);
		}
		return lhmKumpiScaled;
	}

	private double checkTheBiggestValue(LinkedHashMap<String, Double> lhmYla, LinkedHashMap<String, Double> lhmAla) {
		double palautus = 0;
		Iterator ity = lhmYla.keySet().iterator();
		while(ity.hasNext()){
			String key = ity.next().toString();
			double mahdollinen = lhmYla.get(key);
			if(mahdollinen > palautus){
				palautus = mahdollinen;
			}
		}
		Iterator ita = lhmAla.keySet().iterator();
		while(ita.hasNext()){
			String key = ita.next().toString();
			double mahdollinen2 = lhmAla.get(key);
			if(mahdollinen2 > palautus){
				palautus = mahdollinen2;
			}
		}
		return palautus;
	}
	
}